﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="XmlComparisonTab.cs" company="" />
// <summary>
//   A tab-page for presenting XML comparison between two XML sources.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Xompare
{
    using System;
    using System.Windows.Forms;

    using FormsUtilities;

    using Xompare.Helpers;

    /// <summary>
    /// A tab-page for presenting XML comparison between two XML sources.
    /// </summary>
    public class XmlComparisonTabPage : TabPage, IXmlComparisonTabPage
    {
        /// <summary>
        /// The comparison control placed on the tab page.
        /// </summary>
        private XmlFilesCompareControl m_comparisonControl = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="XmlComparisonTabPage"/> class specifying the text to display on its title.
        /// </summary>
        /// <param name="text">The text for the tab page title.</param>
        public XmlComparisonTabPage(string text)
            : base(text)
        {
        }

        /// <summary>
        /// Create a new tab with XML files comparator.
        /// </summary>
        /// <param name="getOriginalFilePath">A func to get the original XML file path.</param>
        /// <param name="getTargetFilePath">A func to get the target XML file path.</param>
        /// <param name="ignoreNameCase">Whether to ignore XML objects' name casing.</param>
        /// <param name="ignoreValueCase">Whether to ignore XML objects' value casing.</param>
        /// <param name="ignoreWhiteSpaces">Whether to ignore white spaces on XML names and values.</param>
        /// <returns>Whether the tab is ready with valid XML information.</returns>
        public bool AttachXmlComparison(
            Func<NameAndLabel> getOriginalFilePath,
            Func<NameAndLabel> getTargetFilePath,
            bool ignoreNameCase,
            bool ignoreValueCase,
            bool ignoreWhiteSpaces)
        {
            var isReady = false;

            m_comparisonControl = new XmlFilesCompareControl { Dock = DockStyle.Fill };
            m_comparisonControl.SetComparisonProps(ignoreNameCase, ignoreValueCase, ignoreWhiteSpaces);

            m_comparisonControl.OnComparedFilesChanged +=
                newName =>
                {
                    Text = newName;
                    Controller.SetApplicationTitle(newName);
                };

            var originalFilePath = getOriginalFilePath();

            if (!string.IsNullOrEmpty(originalFilePath.Name))
            {
                m_comparisonControl.OriginalXmlPath = originalFilePath;
                if (!string.IsNullOrEmpty(m_comparisonControl.OriginalXmlPath.Name) && m_comparisonControl.OriginalXmlPath.Name.Equals(originalFilePath.Name))
                {
                    var targetFilePath = getTargetFilePath();
                    if (!string.IsNullOrEmpty(targetFilePath.Name))
                    {
                        m_comparisonControl.TargetXmlPath = targetFilePath;
                        if (!string.IsNullOrEmpty(m_comparisonControl.TargetXmlPath.Name) && m_comparisonControl.TargetXmlPath.Name.Equals(targetFilePath.Name))
                        {
                            Controls.Add(m_comparisonControl);

                            isReady = true;
                        }
                    }
                }
            }

            return isReady;
        }

        #region ITabPageBehavior implementation

        public void RefreshTabPage()
        {
            if (m_comparisonControl != null)
            {
                m_comparisonControl.Reload(force: true);
            }
        }

        public void OnRemoval(bool isLastRemoved)
        {
            if (isLastRemoved)
            {
                UiCommand.GetUiCommand(UiCommandNames.TabXmlComparison).Visible = false;
            }

            m_comparisonControl.OnRemoval();
        }

        #endregion

        #region IXmlTabPage implementation

        public void OnViewXmlSyntax(bool newValue)
        {
        }

        #endregion

        #region IXmlComparisonTabPage implementation

        public void OnIgnoreNameCaseChange(bool newValue)
        {
            m_comparisonControl.Reload(force: false, ignoreNameCase: newValue);
        }

        public void OnIgnoreValueCaseChange(bool newValue)
        {
            m_comparisonControl.Reload(force: false, ignoreValueCase: newValue);
        }

        public void OnIgnoreWhiteSpacesChange(bool newValue)
        {
            m_comparisonControl.Reload(force: false, ignoreWhiteSpaces: newValue);
        }

        public void SoftRefreshTab(bool? ignoreNameCase = null, bool? ignoreValueCase = null, bool? ignoreWhiteSpaces = null)
        {
            m_comparisonControl.Reload(false, ignoreNameCase, ignoreValueCase, ignoreWhiteSpaces);
        }

        #endregion
    }
}
